package cn.dengta.webapp.user.service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import cn.dengta.common.model.Messager;
import cn.dengta.common.model.TimeInterval;
import cn.dengta.common.web.Page;
import cn.dengta.webapp.base.service.RemoteBaseService;
import cn.dengta.webapp.user.dto.UserMenuPermissionDTO;
import cn.dengta.webapp.user.entity.*;
import cn.dengta.webapp.user.form.AddVipTimeForm;
import me.codeplayer.util.EasyDate;
import me.codeplayer.util.TimeBasedCacheLoader;

public interface RemoteUserService extends RemoteBaseService<User, Long> {

	default Map<Long, UserMenuPermissionDTO> userMenuLoader(boolean flush) {
		Supplier<Map<Long, UserMenuPermissionDTO>> loader = MemberServiceImpl.userMenuLoader;
		if (flush || loader == null) {
			MemberServiceImpl.userMenuLoader = loader = new TimeBasedCacheLoader<>(5 * EasyDate.MILLIS_OF_HOUR, ConcurrentHashMap::new);
		}
		return loader.get();
	}

	default UserMenuPermissionDTO getPermissions(Long userId) {
		return userMenuLoader(false).get(userId);
	}

	default void putPermissions(Long userId, UserMenuPermissionDTO permissions) {
		userMenuLoader(false).put(userId, permissions);
	}

	/**
	 * 根据查询条件获取分页
	 */
	Page<User> findPage(Page<User> page, User input, TimeInterval addTimeInterval);

	/**
	 * 增加用户VIP时长
	 *
	 * @param form 通过充值内部赋值
	 */
	Messager<User> addVipTime(AddVipTimeForm form);

	/**
	 * 增加额外展示时长时长
	 */
	Messager<Long> addExtraRecUpTime(RecExposureLog log);

	List<User> findByIds(Boolean isAnonymous, Collection<Long> ids);

	/**
	 * 获取菜单
	 */
	UserMenuPermissionDTO findPermissionCodesByUserId(Long userId);

	/**
	 * 更新用户sort分值
	 */
	void updateSortById(Long userId, int score);

	/** 获取用户粉丝数 */
	Long findUserFollow(Long userId);

	/** 批量获取 */
	List<Relation> findUserFollowUsers(Long userId, long minId, int batch);

	User updateBizFlag(Long userId, long bizFlag, boolean addOrDel);

	/**
	 * 加入推荐白名单
	 *
	 * @param userIds 用户ID
	 * @param isAdd 是否新增
	 */
	void addRecommendWhite(Set<Long> userIds, boolean isAdd);

	/**
	 * 退出推荐
	 */
	void outRec(Long userId);

	/**
	 * 进入推荐
	 */
	void inRec(Long userId);

	/**
	 * 注销
	 */
	Messager<String> destroy(Long userId);


}
